<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.ui.tree.TreeControl</title>
<script type="text/javascript" src="../../base.js"></script>
<script type="text/javascript">
  goog.require('goog.testing.jsunit');
  goog.require('goog.dom');
  goog.require('goog.events.KeyCodes');
  goog.require('goog.ui.tree.TreeControl');
</script>
</head>
<body>
  <div id='treeContainer' style="width:400px"></div>
<script type="text/javascript">

function makeATree() {
  var tree = new goog.ui.tree.TreeControl('root');
  var testData = ['A', [
                  ['AA', [
                    ['AAA', []],
                    ['AAB', []]
                  ]],
                  ['AB', [
                    ['ABA', []],
                    ['ABB', []]
                  ]]
                ]];

  createTreeFromTestData(tree, testData, 3);
  tree.render(goog.dom.getElement('treeContainer'));
  return tree;
};

function createTreeFromTestData(node, data, maxLevels) {
  node.setHtml(data[0]);
  if (maxLevels < 0) {
    return;
  }

  var children = data[1];
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var childNode = node.getTree().createNode('');
    node.add(childNode);
    createTreeFromTestData(childNode, child, maxLevels - 1);
  }
};

/**
 * Test moving a node to a greater depth.
 */
function testIndent() {
  var tree = makeATree();
  tree.expandAll();

  var node = tree.getChildren()[0].getChildren()[0];
  assertEquals(node.html_,'AAA');
  assertNotNull(node.getElement());
  assertEquals('19px', node.getRowElement().style.paddingLeft);

  assertEquals(2, node.getDepth());

  var newParent = node.getNextSibling();
  assertEquals('AAB', newParent.html_);
  assertEquals(2, newParent.getDepth());

  newParent.add(node);

  assertEquals(newParent, node.getParent());
  assertEquals(node, newParent.getChildren()[0]);
  assertEquals(3, node.getDepth());
  assertEquals('38px', node.getRowElement().style.paddingLeft);
};

/**
 * Test moving a node to a lesser depth.
 */
function testOutdent() {
  var tree = makeATree();
  tree.expandAll();

  var node = tree.getChildren()[0].getChildren()[0];
  assertEquals(node.html_,'AAA');
  assertNotNull(node.getElement());
  assertEquals('19px', node.getRowElement().style.paddingLeft);

  assertEquals(2, node.getDepth());

  var newParent = tree;
  assertEquals('A', newParent.html_);
  assertEquals(0, newParent.getDepth());

  newParent.add(node);

  assertEquals(newParent, node.getParent());
  assertEquals(node, newParent.getChildren()[2]);
  assertEquals(1, node.getDepth());
  assertEquals('0px', node.getRowElement().style.paddingLeft);
};

</script>
</body>
</html>
